﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sunboxsoft.Bases.Web.Models
{
    public class Breadcrumb
    {
        public string IconUrl { get; set; }
        public string Icon { get; set; }
        public string Text { get; set; }
        public string LinkUrl { get; set; }

        public bool IsActived { get; set; }

        /// <summary>
        /// url正则匹配规则数组，符合这个规则话就激活这个菜单
        /// </summary>
        public string[] UrlRegexArrage { get; set; }

        public Breadcrumb(Menu menu)
        {
            IconUrl = menu.IconUrl;
            Icon = menu.Icon;
            Text = menu.Text;
            LinkUrl = menu.LinkUrl;
            IsActived = menu.IsActived;
            UrlRegexArrage = menu.UrlRegexArrage;
        }
    }


    public class BreadcrumbResolver
    {
        public static string BaseUrl = System.Configuration.ConfigurationManager.AppSettings["BaseUrl"];

        public static List<Breadcrumb> Resolve(List<Menu> menus, Uri currentUrl)
        {
            List<Breadcrumb> list = new List<Breadcrumb>();
            Stack<Menu> pass = new Stack<Menu>();
            Stack<Menu> stack = new Stack<Menu>();
            foreach (var menu in menus)
            {
                if (Resolve(menu, currentUrl, list))
                {
                    break;
                }
            }
            return list;
        }

        private static bool Resolve(Menu menu, Uri currentUrl, List<Breadcrumb> list)
        {
            if (menu.Children == null || menu.Children.Count == 0)
            {
                if(IsSameUrl(menu.LinkUrl, currentUrl))
                {
                    list.Insert(0, new Breadcrumb(menu));
                    return true;
                }
                return false;
            }
            else
            {
                foreach (var childMenu in menu.Children)
                {
                    if (Resolve(childMenu, currentUrl, list))
                    {
                        list.Insert(0, new Breadcrumb(menu));
                        return true;
                    }
                }
            }
            return false;
        }

        private static bool IsSameUrl(string url1, Uri url2)
        {
            if (string.IsNullOrEmpty(url1))
            {
                return false;
            }
            if (string.Equals(url1, "#"))
            {
                return false;
            }

            Uri actualUrl1;
            if (Uri.TryCreate(new Uri(BaseUrl), url1, out actualUrl1) == false)
            {
                return false;
            }
            if (string.Equals(actualUrl1.LocalPath, url2.LocalPath, StringComparison.CurrentCultureIgnoreCase))
            {
                return true;
            }
            return false;
        }

        private static string GetAbsoluteUrl(string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                return string.Empty;
            }

            Uri actualUrl;
            if (Uri.TryCreate(new Uri(BaseUrl), url, out actualUrl))
            {
                return actualUrl.ToString();
            }
            return url;
        }
    }
}
